<template>
  <div class="admin-applications">
    <div class="page-header">
      <h1>报名管理</h1>
      <p>管理学生报名申请信息</p>
    </div>

    <!-- 搜索筛选 -->
    <el-card class="filter-card" shadow="never">
      <el-form :model="searchForm" :inline="true">
        <el-form-item label="院校">
          <el-select v-model="searchForm.schoolId" placeholder="选择院校" clearable>
            <el-option v-for="school in schools" :key="school.id" :label="school.name" :value="school.id" />
          </el-select>
        </el-form-item>
        <el-form-item label="专业">
          <el-select v-model="searchForm.majorId" placeholder="选择专业" clearable>
            <el-option v-for="major in majors" :key="major.id" :label="major.name" :value="major.id" />
          </el-select>
        </el-form-item>
        <el-form-item label="状态">
          <el-select v-model="searchForm.status" placeholder="申请状态" clearable>
            <el-option label="已提交" value="submitted" />
            <el-option label="已审核" value="approved" />
            <el-option label="已拒绝" value="rejected" />
          </el-select>
        </el-form-item>
        <el-form-item label="关键词">
          <el-input v-model="searchForm.keyword" placeholder="姓名/身份证" clearable />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="loadApplications">搜索</el-button>
          <el-button @click="resetSearch">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <!-- 数据表格 -->
    <el-card shadow="never">
      <template #header>
        <div class="card-header">
          <span>报名申请列表</span>
          <div>
            <el-button type="success" @click="exportData">导出数据</el-button>
          </div>
        </div>
      </template>

      <el-table v-loading="loading" :data="applications" stripe>
        <el-table-column prop="studentName" label="学生姓名" />
        <el-table-column prop="idCard" label="身份证号" />
        <el-table-column prop="phone" label="联系电话" />
        <el-table-column prop="schoolName" label="报考院校" />
        <el-table-column prop="majorName" label="报考专业" />
        <el-table-column prop="totalScore" label="总分" />
        <el-table-column prop="applicationStatus" label="申请状态">
          <template #default="{ row }">
            <el-tag :type="getStatusType(row.applicationStatus)">
              {{ getStatusText(row.applicationStatus) }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="paymentStatus" label="缴费状态">
          <template #default="{ row }">
            <el-tag :type="getPaymentType(row.paymentStatus)">
              {{ getPaymentText(row.paymentStatus) }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="applicationTime" label="申请时间" />
        <el-table-column label="操作" width="200">
          <template #default="{ row }">
            <el-button size="small" @click="viewDetail(row)">查看</el-button>
            <el-button size="small" type="primary" @click="editStatus(row)">审核</el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <div class="pagination-wrapper">
        <el-pagination
          v-model:current-page="pagination.page"
          v-model:page-size="pagination.size"
          :total="pagination.total"
          :page-sizes="[10, 20, 50, 100]"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="loadApplications"
          @current-change="loadApplications"
        />
      </div>
    </el-card>

    <!-- 审核状态对话框 -->
    <el-dialog v-model="statusDialogVisible" title="审核申请" width="500px">
      <el-form :model="statusForm" label-width="100px">
        <el-form-item label="申请状态">
          <el-select v-model="statusForm.status">
            <el-option label="已审核" value="approved" />
            <el-option label="已拒绝" value="rejected" />
          </el-select>
        </el-form-item>
        <el-form-item label="备注">
          <el-input v-model="statusForm.remark" type="textarea" :rows="4" />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="statusDialogVisible = false">取消</el-button>
        <el-button type="primary" @click="updateStatus">确定</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { ElMessage } from 'element-plus'
import api from '@/api'

// 响应式数据
const loading = ref(false)
const applications = ref([])
const schools = ref([])
const majors = ref([])

const searchForm = reactive({
  schoolId: '',
  majorId: '',
  status: '',
  keyword: ''
})

const pagination = reactive({
  page: 1,
  size: 20,
  total: 0
})

const statusDialogVisible = ref(false)
const statusForm = reactive({
  id: null,
  status: '',
  remark: ''
})

// 方法
const loadApplications = async () => {
  loading.value = true
  try {
    const params = {
      page: pagination.page,
      size: pagination.size,
      ...searchForm
    }
    const { data } = await api.get('/enrollment/applications', { params })
    applications.value = data.records || []
    pagination.total = data.total || 0
  } catch (error) {
    ElMessage.error('加载失败')
  } finally {
    loading.value = false
  }
}

const loadSchools = async () => {
  try {
    const { data } = await api.get('/schools')
    schools.value = data.records || []
  } catch (error) {
    console.error('加载院校失败:', error)
  }
}

const loadMajors = async () => {
  if (searchForm.schoolId) {
    try {
      const { data } = await api.get('/majors', { 
        params: { schoolId: searchForm.schoolId } 
      })
      majors.value = data.records || []
    } catch (error) {
      console.error('加载专业失败:', error)
    }
  } else {
    majors.value = []
  }
}

const resetSearch = () => {
  Object.assign(searchForm, {
    schoolId: '',
    majorId: '',
    status: '',
    keyword: ''
  })
  majors.value = []
  loadApplications()
}

const viewDetail = (row) => {
  ElMessage.info('查看详情功能待实现')
}

const editStatus = (row) => {
  statusForm.id = row.id
  statusForm.status = row.applicationStatus
  statusForm.remark = row.remark || ''
  statusDialogVisible.value = true
}

const updateStatus = async () => {
  try {
    await api.put(`/enrollment/applications/${statusForm.id}/status`, null, {
      params: {
        status: statusForm.status,
        remark: statusForm.remark
      }
    })
    ElMessage.success('状态更新成功')
    statusDialogVisible.value = false
    loadApplications()
  } catch (error) {
    ElMessage.error('状态更新失败')
  }
}

const exportData = () => {
  ElMessage.info('导出功能待实现')
}

const getStatusType = (status) => {
  const map = {
    submitted: 'warning',
    approved: 'success',
    rejected: 'danger'
  }
  return map[status] || 'info'
}

const getStatusText = (status) => {
  const map = {
    submitted: '已提交',
    approved: '已审核',
    rejected: '已拒绝'
  }
  return map[status] || status
}

const getPaymentType = (status) => {
  const map = {
    pending: 'warning',
    paid: 'success'
  }
  return map[status] || 'info'
}

const getPaymentText = (status) => {
  const map = {
    pending: '待缴费',
    paid: '已缴费'
  }
  return map[status] || status
}

// 监听院校变化
watch(() => searchForm.schoolId, () => {
  searchForm.majorId = ''
  loadMajors()
})

// 初始化
onMounted(() => {
  loadApplications()
  loadSchools()
})
</script>

<style lang="scss" scoped>
.admin-applications {
  .page-header {
    margin-bottom: 20px;
    
    h1 {
      margin: 0 0 8px 0;
      font-size: 24px;
      color: #303133;
    }
    
    p {
      margin: 0;
      color: #909399;
    }
  }
  
  .filter-card {
    margin-bottom: 20px;
  }
  
  .card-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
  }
  
  .pagination-wrapper {
    margin-top: 20px;
    text-align: right;
  }
}
</style>